package com.cn.demo.springboot.shiro.mapper;

import java.util.Date;
import java.util.List;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import com.cn.demo.springboot.shiro.entity.Permission;
import com.cn.demo.springboot.shiro.entity.Role;

@Mapper
public interface RoleMapper {
	
	
	/**
	 * 
	 * @param permissionId 权限ID
	 * @param roleId 角色id
	 * @param createTime 创建时间
	 * @param createUserId 创建人
	 */
	@Insert("insert into sys_role_permission (permission_Id," + 
			"roleId," + 
			"deleted," + 
			"create_Time," + 
			"update_Time," + 
			"create_User_Id," + 
			"update_User_Id" + 
			") value (#{permissionId},#{roleId},0,#{createTime},#{createTime},#{createUserId},#{createUserId})" )
	public void insertRolePermission(@Param("permissionId")Integer permissionId,
			@Param("roleId")Integer roleId,@Param("createTime")Date createTime,
			@Param("createUserId")String createUserId);
	
	
	@Delete("update  sys_role_permission set deleted = 1,update_Time = #{updateTime},update_User_Id = #{updateUserId} where roleId = #{roleId},#{permissionId}")
	public void removeRolePermission(@Param("roleId")Integer roleId,@Param("permissionId")Integer permissionId,@Param("updateTime")Date updateTime,
			@Param("updateUserId")String updateUserId);
	
	@Select("select (p.permission_Id),p.* from sys_user_role ur ,sys_role_permission rp , sys_permission p where ur.user_id = #{fuckyou} and ur.role_id = rp.roleId and rp.permission_id = p.permission_id")
	public List<Permission> findPermissionByUser(String userId);
	
	@Insert("insert into sys_role (role_Name,deleted,create_Time,update_Time,create_User_Id,update_User_Id) value(#{roleName},0,#{createTime},#{updateTime},#{createUserId},#{updateUserId})")
	public void addRole(Role role);
	
	@Update("update sys_role set deleted  = 1 ,update_Time = #{updateTime},current_User_Id = #{currentUserId} where role_id = #{roleId}")
	public void delRole(@Param("roleId")Integer roleId,@Param("updateTime")Date updateTime,@Param("currentUserId")String currentUserId);
	
	@Insert("insert into sys_user_role (user_Id," + 
			"role_Id," + 
			"deleted," + 
			"create_Time," + 
			"update_Time," + 
			"create_User_Id," + 
			"update_User_Id" + 
			") value (#{userId},#{roleId},0,#{createTime},#{createTime},#{createUserId},#{createUserId})")
	public void addUserRole(@Param("userId")String userId,@Param("roleId")Integer roleId,@Param("createTime")Date createTime,@Param("createUserId")String createUserId);
	
	@Update("update sys_user_role set deleted = 1,update_Time = now(),update_User_Id = #{updateUserId} where user_id =#{userId} and role_id = #{roleId} ")
	public void removeUserRole(@Param("userId")String userId,@Param("updateUserId")String updateUserId,@Param("roleId")Integer  roleId);

	@Select("Select * from sys_role")
	public List<Role> findRoleAll( );

	

}
